<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>借货出库记录审批列表</title>
    <script type="text/javascript">
      var loanDeliveryAuditForAddWin;
      var loanDeliveryAuditForCancelWin;
      var loanDeliveryAuditId;
      Ext.onReady(function(){

        var customerTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<div style="float:left; width:70px">{number}</div>|{name}', '</div></tpl>');
        var customerStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/customer/getAllCustomers?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number',
              convert:function(value,record){
                if (value === ""){
                  return "未设置编号"
                }else{
                  return value;
                }
              }
            }, {
              name: 'name'
            }])
        });

        var loanNumber = new Ext.form.TextField({
          fieldLabel: '借货单号',
          name: 'loan_order_number',
          anchor: '95%'
        });

        var orderNumber = new Ext.form.TextField({
          fieldLabel: '借货出库单号',
          name: 'order_number',
          anchor: '95%'
        });

        var customer =  new Ext.form.ComboBox({
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'customer_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          pageSize: 25,
          //hideTrigger: false,
          tpl: customerTemplate,
          minChars: 0,
          anchor: '100%',
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        var beginDate = new Ext.form.DateField({
          fieldLabel: '借货出库日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });

        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });

        var submitButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            loanDeliveryAuditDS.removeAll();
            loanDeliveryAuditDS.baseParams.order_number = orderNumber.getValue();
            loanDeliveryAuditDS.baseParams.loan_number = loanNumber.getValue();

            loanDeliveryAuditDS.baseParams.begin_date = beginDate.getRawValue();
            loanDeliveryAuditDS.baseParams.end_date = endDate.getRawValue();
            loanDeliveryAuditDS.baseParams.customer_id = customer.getValue();
            loanDeliveryAuditDS.load();
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 80,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1000},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [loanNumber]
                    }, {
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [orderNumber]
                    }, {
                      columnWidth: .35,
                      layout: 'form',
                      border: false,
                      items: [customer]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [beginDate]
                    }, {
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [endDate]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示借货出库操作时的窗口介面。
        //----------------------------------------------------------------------
        //借货出库Grid Store
        var loanDeliveryAuditDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/loan_delivery/getAllLoanDeliveryByAuditStatus'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'auditing_status'
            },{
              name: 'loan_number',
              mapping:'loan.order_number'
            },{
              name: 'loan_status',
              mapping:'loan.auditing_status'
            },{
              name: 'order_number'
            },{
              name: 'loan_action_date',
              mapping:'loan.action_date'
            }, {
              name: 'action_date'
            }, {
              name: 'description'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            },{
              name: 'department_name',
              mapping:'department.name'
            }, {
              name: 'warehouse_name',
              mapping:'warehouse.name'
            }, {
              name: 'customer_name',
              mapping:'customer.name'
            },{
              name:'deal_with_status'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        
        //生成借货出库Grid的CM
        var loanDeliveryAuditSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var loanDeliveryAuditCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),loanDeliveryAuditSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '借货订单单号',
              dataIndex: 'loan_number',
              width: 170
            },{
              header: '借货订单状态',
              dataIndex: 'loan_status',
              width: 85,
              renderer: auditingStatusRender
            },{
              header: '借货出库单单号',
              dataIndex: 'order_number',
              width: 120
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 80,
              renderer: auditingStatusRender
            },{
              header: '调出仓库',
              dataIndex: 'warehouse_name',
              width: 90
            },{
              header: '到客户',
              dataIndex: 'customer_name',
              width: 190
            },{
              header: '要求借货出库日期',
              dataIndex: 'loan_action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 110
            },{
              header: '实际借货出库日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 110
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 70
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '订单状态',
              dataIndex: 'deal_with_status',
              width: 80,
              renderer: auditingStatusRender
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            }
          ]});

        //生成要订单的产品的Grid
        var loanDeliveryAuditGrid = new Ext.grid.GridPanel({
          title:'借货出库单列表',
          store: loanDeliveryAuditDS,
          cm: loanDeliveryAuditCM,
          sm: loanDeliveryAuditSM,
          loadMask: true,
          //renderTo: 'delivery_list_grid',
          width: screenWidth-175,
          height: 345,
          frame: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: loanDeliveryAuditDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })          
        });
        loanDeliveryAuditDS.load();

        loanDeliveryAuditGrid.on('rowdblclick', function(gridPanel, rowIndex, e){
          loanDeliveryAuditId = loanDeliveryAuditDS.getAt(rowIndex).get('id');
          var loanDeliveryAuditNumber = loanDeliveryAuditDS.getAt(rowIndex).get('order_number');
          //loanDeliveryAuditDS.reload();
          loanDeliveryAuditDetailDS.removeAll();
          loanDeliveryAuditDetailDS.baseParams.loan_delivery_id = loanDeliveryAuditId;
          loanDeliveryAuditDetailDS.load();
          loanDeliveryAuditDetailGrid.setTitle("借货订单："+loanDeliveryAuditNumber+"，的详细信息");
        });

        loanDeliveryAuditGrid.on('rowcontextmenu', function(grid, rowIndex, e){
          //初始化所有右键菜单为不可用
          auditForDeliveryItem.setDisabled(true);
          auditForCancelItem.setDisabled(true);
          loanDeliveryId = loanDeliveryAuditDS.getAt(rowIndex).get('id');
          currentOrderNumber = loanDeliveryAuditDS.getAt(rowIndex).get('order_number');
          var auditingStatus = loanDeliveryAuditDS.getAt(rowIndex).get('auditing_status');
          if (auditingStatus.indexOf("申请新增") > -1){
            auditForDeliveryItem.setDisabled(false);
          }else if (auditingStatus.indexOf("申请撤消") > -1){
            auditForCancelItem.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        //############ 订单-右键菜单 ##########
        var auditForDeliveryItem = new Ext.menu.Item({
          text: '【审批新增借货出库单】',
          //iconCls: 'album',
          handler: rightMenuAuditDeliveryFn
        });

        function rightMenuAuditDeliveryFn(){
          loanDeliveryAuditForAddWin = new Ext.Window({
            layout: 'fit',
            width:387,
            height: 270,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '审批新增借货出库单：'+currentOrderNumber,
            autoLoad: {
              url: "/loan_delivery/auditLoanDelivery?id="+loanDeliveryId,
              scripts: true
            }
          });
          loanDeliveryAuditForAddWin.on("destroy",function(){
            loanDeliveryAuditDS.reload();
          });
          loanDeliveryAuditForAddWin.resizable = false;
          loanDeliveryAuditForAddWin.show();
        }

        var auditForCancelItem = new Ext.menu.Item({
          text: '【审批撤消借货出库单】',
          //iconCls: 'album',
          handler: rightMenuAuditDeliveryCancelFn
        });

        function rightMenuAuditDeliveryCancelFn(){
          loanDeliveryAuditForCancelWin = new Ext.Window({
            layout: 'fit',
            width:387,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '审批<font color=red>撤消</font>借货出库单：'+currentOrderNumber,
            autoLoad: {
              url: "/loan_delivery/auditDeliveryOrderForCancel?id="+loanDeliveryId,
              scripts: true
            }
          });
          loanDeliveryAuditForCancelWin.on("destroy",function(){
            loanDeliveryAuditDS.reload();
          });
          loanDeliveryAuditForCancelWin.resizable = false;
          loanDeliveryAuditForCancelWin.show();
        }
        
        var rightClick = new Ext.menu.Menu({
          items: [auditForDeliveryItem,auditForCancelItem]
        });
        
        //--------------------------借货订单详细，双击后可显示数据----------------
        //借货订单Grid Store Detail
        var loanDeliveryAuditDetailDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/loan_delivery/getDeliveryOrderDetailsById'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_spec',
              mapping:'product.spec'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }, {
              name: 'description'
            }])
        });

        //生成借货出库单详细表的Grid的CM
        var loanDeliveryAuditDetailSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var loanDeliveryAuditDetailCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),loanDeliveryAuditDetailSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 210
            }, {
              header: '产品编号',
              dataIndex: 'product_number',
              width:95
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 80
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }
          ]});
        //生成要订单的产品的Grid
        var loanDeliveryAuditDetailGrid = new Ext.grid.GridPanel({
          title:'借货出库单详细列表',
          store: loanDeliveryAuditDetailDS,
          cm: loanDeliveryAuditDetailCM,
          sm: loanDeliveryAuditDetailSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-640,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-170,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[queryFormPanel,loanDeliveryAuditGrid,loanDeliveryAuditDetailGrid]
        });
        workbenchPanel.render("delivery_list");
        
      })
    </script>
  </head>
  <body>
    <div id="delivery_list"></div>
  </body>
</html>
